<?php

/**
 * @file searching_grid.php
 * @brief Clase para generar un searching_grid
 * @author DyrTechnologies
 * @version V 1.1
 * @date Wed Oct 03 2007
 *
 *  Part of the CEFIRO dynamic web framework project
 *
 *  Copyright (C) 2007  Dyr technologies http://dyrtech.com.ve
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class Clase searching_grid
 */
if (!defined('CEFIRO')) define("CEFIRO",substr(__FILE__,0,strpos(__FILE__,"cefiro"))."cefiro/widgets/cefiro.php");
require_once(CEFIRO);

require_once(CEF_INPUT_OPT);
require_once(CEF_DOMAINS);

/**
 * @brief Define la ruta a la hoja de estilos para el searching_grid
 *
 */

define("SEARCHING_GRID_CSS",FULL_URL."searching_grid/css/searching_grid.css");
define("SEARCHING_GRID_JS",FULL_URL."searching_grid/js/searching_grid.js");
define("SEARCHING_GRID_IMG",FULL_URL."searching_grid/img/");
define("GRID_IMG",FULL_URL."grid/img/");
define("GRID_CSS",FULL_URL."grid/css/dhtmlXGrid.css");
define("COMMON_JS",FULL_URL."grid/js/dhtmlXCommon.js");
define("GRID_JS",FULL_URL."grid/js/dhtmlXGrid.js");
define("GRID_CELL_JS",FULL_URL."grid/js/dhtmlXGridCell.js");
define("MOVE_JS",FULL_URL."searching_grid/js/move.js");
define("NOTIFICACION_JS",FULL_URL."searching_grid/js/notificacion.js");

$searching_grid_instance = 0;

class searching_grid extends input_opt {

	private $value;
	private $name;
	private $label;
	private $first_input_field;
	private $second_input_field;
	private $third_input_field;
	private $id_db_field;
	private $first_db_field;
	private $second_db_field;
	private $third_db_field;
	private $search_field;
	private $domain;
	private $query;
	private $env;
	private $sg_parent;
	private $display_fields;
	private $fields_size;
	private $idvalue;
	private $extra_args;
	public $show_scripts;

	/**
	 * @brief Constructor por omision y parametrico de la clase aprovechamos el hecho de que cada atributo tiene un valor por omision predefinido para no rehacer el constructor
	 *
	 * @param string $_value
	 * @param string $_label
	 * @param string $_name
	 * @param string $_id_db_field
	 * @param string $_first_db_field
	 * @param string $_second_db_field
	 * @param string $_third_db_field
	 * @param string $_first_input_field
	 * @param string $_second_input_field
	 * @param string $_third_input_field
	 * @param input_opt $_opt
	 * @param domain $_dom
	 * @return searching_grid La instancia de la clase
	 */

	public function searching_grid($_name='__searching_grid', $_label='', $_value='', $_first_input_field = '', $_second_input_field='', $_third_input_field='', $_id_db_field = '', $_first_db_field = '', $_second_db_field='',  $_third_db_field='', $_search_field='',$_opt = '', $_dom = '', $_query='', $_env=".", $_sg_parent='', $_show_script='true', $_idvalue=''){

		/**
		 * @brief Se asignan los valores a los atributos de la clase searching_grid segun vengan dados al llamar al constructor de la misma, notar que existen valores por omision para cada atributo
		 */
		$this->value=$_value;
		$this->label=$_label;
		$this->first_input_field=$_first_input_field;
		$this->second_input_field=$_second_input_field;
		$this->third_input_field=$_third_input_field;
		$this->id_db_field=$_id_db_field;
		$this->first_db_field=$_first_db_field;
		$this->second_db_field=$_second_db_field;
		$this->third_db_field=$_third_db_field;
		$this->search_field=$_search_field;
		$this->query=$_query;
		$this->env=$_env;
		$this->sg_parent=$_sg_parent;
		$this->idvalue = $_idvalue;
		$this->show_scripts=$_show_script;

		/**
		 * @brief $searching_grid_instance es una variable para incrementar el numero de searching_grides existentes
		 */
		global $searching_grid_instance;
		$searching_grid_instance++;

		/**
		 * @brief El nombre del widget es el tipo con el numero de la instancia que representa o el nombre pasado por omision en caso contrario
		 */

		if($_name!="__searching_grid")
		$this->name=$_name;
		else
		$this->name="searching_grid".$searching_grid_instance;

		/**
		 * @brief Si las opciones del campo no estan ajustadas lo hacemos explicitamente una instancia de input_opt
		 */
		if($_opt=='')
		$_opt = new input_opt();
		$this->equalize($_opt);

		$this->domain = new domain();

		if($_dom!='')
		$this->set_domain($_dom);

		$this->display_fields = array("false","false","false");
		$this->fields_size = array(70,140,140);
		$this->extra_args =array();
	}


	/**
	 * @brief Las funciones get_VAR devuelven las variables que tenga ajustadas la instancia: valor (value), Nombre (name), Etiqueta (label), opciones de entrada (opt)
	 *
	 * @return string Valor de VAR
	 */

	public function get_name(){
		return $this->name;
	}

	public function get_value(){
		return $this->value;
	}

	public function get_first_input_field(){
		return $this->first_input_field;
	}

	public function get_second_input_field(){
		return $this->second_input_field;
	}

	public function get_third_input_field(){
		return $this->third_input_field;
	}

	public function get_id_db_field(){
		return $this->id_db_field;
	}

	public function get_first_db_field(){
		return $this->first_db_field;
	}

	public function get_second_db_field(){
		return $this->second_db_field;
	}

	public function get_third_db_field(){
		return $this->third_db_field;
	}

	public function get_search_field(){
		return $this->search_field;
	}

	public function get_label(){
		return $this->label;
	}

	public function get_query(){
		return $this->query;
	}

	public function get_env(){
		return $this->env;
	}

	public function get_opt(){
		return $this->get_input_opt();
	}

	public function get_display_fields(){
		return $this->display_fields;
	}

	public function get_fields_size(){
		return $this->fields_size;
	}

	public function get_sg_parent(){
		if(strlen($this->sg_parent)>0)
			return $this->sg_parent.".";
		else return $this->sg_parent;
	}

	public function get_idvalue(){
		return $this->idvalue;
	}

	public function get_extra_args(){
		return $this->extra_args;
	}

	/**
	 * @brief Las funciones set_VAR ajustan las variables de la instancia: valor (value), Nombre (name), Etiqueta (label), opciones de entrada (opt)
	 */

	public function set_name($_name){
		$this->name=$_name;
	}

	public function set_value($_value){
		$this->value=$_value;
	}

	public function set_label($_label){
		$this->label=$_label;
	}

	public function set_query($_query){
		$this->query=$_query;
	}

	public function set_env($_env){
		$this->env=$_env;
	}

	public function set_sg_parent($_sg_parent){
		$this->sg_parent=$_sg_parent;
	}

	public function set_first_input_field($_first_input_field, $display = false, $size = 70){
		if($display)
			$this->display_fields[0]="true";
		$this->fields_size[0] = $size;
		$this->first_input_field=$_first_input_field;
	}

	public function set_second_input_field($_second_input_field, $display = false, $size = 140){
		if($display)
			$this->display_fields[1]="true";
		$this->fields_size[1] = $size;
		$this->second_input_field=$_second_input_field;
	}

	public function set_third_input_field($_third_input_field, $display = false, $size = 140){
		if($display)
			$this->display_fields[2]="true";
		$this->fields_size[2] = $size;
		$this->third_input_field=$_third_input_field;
	}

	public function set_id_db_field($_id_db_field){
		$this->id_db_field=$_id_db_field;
	}

	public function set_first_db_field($_first_db_field){
		$this->first_db_field=$_first_db_field;
	}

	public function set_second_db_field($_second_db_field){
		$this->second_db_field=$_second_db_field;
	}

	public function set_third_db_field($_third_db_field){
		$this->third_db_field=$_third_db_field;
	}

	public function set_search_field($_search_field){
		$this->search_field=$_search_field;
	}

	public function set_display_fields($_display_fields){
		$this->display_fields=$_display_fields;
	}

	public function set_fields_size($_fields_size){
		$this->fields_size=$_fields_size;
	}

	public function set_opt($_opt=''){
		if($_opt=='')
		$_opt = new input_opt();
		$this->equalize($_opt);
	}

	public function set_idvalue($_idvalue){
		$this->idvalue = $_idvalue;
	}

	public function set_extra_args($_extra_args){
		$i = 0;
		foreach ($_extra_args as $arg)
			$this->extra_args[$i++] = $arg;
	}

	/**
	 * @brief Reeimplementamos las funciones del dominios... para facilidad al codificar en el nivel de interfaz
	 */

	public function set_domain($nu_domain){
		$this->domain->set_domain($nu_domain);
	}

	public function reset_domain(){
		$this->domain->reset();
	}

	public function add_domain($nu_domain){
		$this->domain->add_domain($nu_domain);
	}

	public function add_domains($nu_domains){
		$this->domain->add_domains($nu_domains);
	}

	public function remove_domain($nu_domain){
		$this->domain->remove_domain($nu_domain);
	}

	public function remove_domains($nu_domains){
		$this->domain->remove_domains($nu_domains);
	}

	public function get_domains(){
		return $this->domain->get_domains();
	}

	/**
	 * @brief Ajusta los valores de smarty de acuerdo a lo especificado por los atributos de la instancia searching_grid
	 */
	private function smarty_searching_grid(){

		global $smarty;
		global $searching_grid_instance;

		/**
		 * @brief Asignacion atributo por atributo a lo deseado en las variables de smarty
		 */
		$smarty->assign("name",$this->get_name());
		$smarty->assign("value",$this->get_value());
		$smarty->assign("idvalue",$this->get_idvalue());
		$smarty->assign("label",$this->get_label());
		$smarty->assign("query",$this->get_query());
		$smarty->assign("env",str_replace("\\","/",$this->get_env()));
		$smarty->assign("parent",$this->get_sg_parent());
		$smarty->assign("first_input_field",$this->get_first_input_field());
		$smarty->assign("second_input_field",$this->get_second_input_field());
		$smarty->assign("third_input_field",$this->get_third_input_field());
		$smarty->assign("id_db_field",$this->get_id_db_field());
		$smarty->assign("first_db_field",$this->get_first_db_field());
		$smarty->assign("second_db_field",$this->get_second_db_field());
		$smarty->assign("third_db_field",$this->get_third_db_field());
		$smarty->assign("search_field",$this->get_search_field());
		$smarty->assign("display_fields",$this->get_display_fields());
		$smarty->assign("fields_size",$this->get_fields_size());
		$smarty->assign("__opt",$this->get_input_opt());
		$smarty->assign("domain",$this->get_domains());
		$smarty->assign("searching_grid_instance",$searching_grid_instance);
		$smarty->assign("show_scripts",$this->show_scripts);
		$smarty->assign("extra_args",$this->get_extra_args());

		/**
		 * @brief Asigna la hoja de estilo que contiene los rasgos del searching_grid a una variable
		 */
		$smarty->assign("searchinggridcss",SEARCHING_GRID_CSS);
		$smarty->assign("searchinggridjs",SEARCHING_GRID_JS);
		$smarty->assign("searchinggridimg",SEARCHING_GRID_IMG);
		$smarty->assign("gridimg",GRID_IMG);
		$smarty->assign("gridcss",GRID_CSS);
		$smarty->assign("commonjs",COMMON_JS);
		$smarty->assign("gridjs",GRID_JS);
		$smarty->assign("gridcelljs",GRID_CELL_JS);
		$smarty->assign("movejs",MOVE_JS);
		$smarty->assign("notificacionjs",NOTIFICACION_JS);
	}

	/**
	 * @brief Asigna el searching_grid a la variable $smarty para su renderizacion
	 *
	 */
	public function display_searching_grid($fetch = false){
		global $smarty;
		$smarty->template_dir = dirname(__FILE__).'/';
		$this->smarty_searching_grid();
		if(!$fetch){
		  $smarty->assign( $this->name , $smarty->fetch('searching_grid.tpl') );
		  return 1;
		}
		else
		  return $smarty->fetch('searching_grid.tpl');
	}

	public function inline_scripts(){
		global $smarty;
		$smarty->template_dir = dirname(__FILE__).'/';
		$smarty->assign("inline_scripts","true");
		$smarty->assign("first_input_field",$this->get_first_input_field());
		$smarty->assign("second_input_field",$this->get_second_input_field());
		$smarty->assign("third_input_field",$this->get_third_input_field());
		$smarty->assign("display_fields",$this->get_display_fields());
		$smarty->assign("fields_size",$this->get_fields_size());
		$smarty->assign("extra_args",$this->get_extra_args());
		$smarty->assign("gridimg",GRID_IMG);
		$smarty->assign("name",$this->get_name());
		return $smarty->fetch('searching_grid.tpl');
	}
}

?>